package com.two.screen.water.balance.ui.fragment.login

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import butterknife.BindView
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.BaseBean
import com.two.screen.water.balance.bean.water.BuyerBean
import com.two.screen.water.balance.bean.water.SellerBean
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.net.api.NormalApi24
import com.two.screen.water.balance.net.observer.DialogObserver
import com.two.screen.water.balance.ui.activity.LoginActivity24
import com.two.screen.water.balance.ui.activity.LoginActivity24.Companion.LOGIN_ID_TYPE_BUYER
import com.two.screen.water.balance.ui.activity.LoginActivity24.Companion.LOGIN_ID_TYPE_SELLER
import com.two.screen.water.balance.ui.fragment.base.BaseFragment
import com.two.screen.water.balance.utils.*
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventBusUtil
import com.two.screen.water.balance.utils.eventbus.EventCode
import java.util.concurrent.TimeUnit

@SuppressLint("NonConstantResourceId")
class LoginPwdFragment(var mIdType: Int) : BaseFragment() {

    override fun setLayoutResouceId(): Int = R.layout.fragment_login_pwd
    @BindView(R.id.mLoginTelEt) lateinit var mLoginTelEt: EditText
    @BindView(R.id.mLoginPasswordEt) lateinit var mLoginPasswordEt: EditText
    @BindView(R.id.mLoginTv) lateinit var mLoginTv: TextView

    override fun initView() {
        if (mIdType == LOGIN_ID_TYPE_BUYER) {
            mLoginPasswordEt.visibility = View.GONE
            mLoginTelEt.setText("17767289171")
        }
    }

    override fun initData(arguments: Bundle?) {
    }

    @SuppressLint("CheckResult")
    override fun initListener() {
        RxView.clicks(mLoginTv)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe {
                onLoginClick()
            }
    }

    private fun onLoginClick() {
        //帐号密码
        var mTel = mLoginTelEt.text.toString()
        var mPwd = mLoginPasswordEt.text.toString()

        if (mTel.isBlank()) {
            ToastUtil.showToast("手机号不能为空！")
            return
        }

        when (mIdType) {
            LOGIN_ID_TYPE_SELLER -> {
                if (mPwd.isBlank()) {
                    ToastUtil.showToast("密码不能为空！")
                    return
                }
                NormalApi24.loginTelPwdSeller(sortedMapOf(Params.sc_n to CommUtils.getAppUUid(), Params.username to mTel, Params.password to /*CommUtils.encodeByMD5()*/mPwd), Urls.leOrMeLogin)
                    .compose(RxUtils.aLifecycle(this))
                    .subscribe(object : DialogObserver<SellerBean>(mActivity, "正在登录...") {
                        override fun onSuccessNext(t: BaseBean<out SellerBean>) {
                            DataApi.mSellerInfo = t.data
                            DataApi.mSellerInfo?.loginTime = CommUtils.toYMD_HMS(System.currentTimeMillis() / 1000)
                            EventBusUtil.sendEvent(Event(EventCode.LOGIN_SUCCESS_SELLER))
                        }
                    })
            }
            LOGIN_ID_TYPE_BUYER -> {
                NormalApi24.loginTelPwdBuyer(sortedMapOf(Params.phone to mTel, Params.bazaarId to DataApi.mSellerInfo?.bazaarList?.firstOrNull()?.id), Urls.selectBuyerByPhone)
                    .compose(RxUtils.aLifecycle(this))
                    .subscribe(object : DialogObserver<BuyerBean>(mActivity, "正在登录...") {
                        override fun onSuccessNext(t: BaseBean<out BuyerBean>) {
                            DataApi.mBuyerInfo = t.data
                            EventBusUtil.sendEvent(Event(EventCode.LOGIN_SUCCESS_BUYER))
                        }
                    })
            }
        }


    }
}
